package model;

public class Car {
	
	private Street curPos;
	private int curPosIndex;
	
	private Street destPos;
	private int destPosIndex;
	
	private Street targetStreet;
	private int routeWeight;
	
	private int skin = (int)(Math.random()*3)+1;
	
	
	//Statistic
	private int waitingTime = 0;
	private boolean moved = false;
	private int idleTime = 0;
	
	
	public int getPriority(){
		return ((idleTime*2) + waitingTime)/(routeWeight+1);
	}	

	public int getWaitingTime() {
		return waitingTime;
	}

	public boolean isMoved() {
		return moved;
	}

	public int getIdleTime() {
		return idleTime;
	}

	public void addWaitingTime() {
		this.waitingTime++;
	}

	public void setMoved(boolean moved) {
		this.moved = moved;
	}

	public void addIdleTime() {
		this.idleTime++;
	}
	
	public Car(){
		
		
	}
	
	public Car(final Street curPos, final int curPosIndex, final Street destPos, final int destPosIndex) {
		super();
		this.curPos = curPos;
		this.curPosIndex = curPosIndex;
		this.destPos = destPos;
		this.destPosIndex = destPosIndex;
	}
	
	public Street getCurPos() {
		return curPos;
	}

	public int getCurPosIndex() {
		return curPosIndex;
	}

	public Street getDestPos() {
		return destPos;
	}

	public int getDestPosIndex() {
		return destPosIndex;
	}
	
	public Street getTargetStreet() {
		return targetStreet;
	}
	
	public int getSkin() {
		return skin;
	}

	public void setTargetStreet(final Street targetStreet) {
		this.targetStreet = targetStreet;
	}

	public void setCurPos(final Street curPos) {
		this.curPos = curPos;
	}

	public void setCurPosIndex(final int curPosIndex) {
		this.curPosIndex = curPosIndex;
	}

	public void setDestPos(final Street destPos) {
		this.destPos = destPos;
	}

	public void setDestPosIndex(final int destPosIndex) {
		this.destPosIndex = destPosIndex;
	}

	public boolean isAtCrossRoad() {
		if(this.getCurPosIndex() == Config.nCellsPalace - 1)
			return true;
		return false;
	}

	public void moveForward() {
		this.curPosIndex++;
		if(curPosIndex >= Config.nCellsPalace)
			this.curPosIndex = Config.nCellsPalace-1;
		this.setMoved(true);
	}

	public void setRouteWeight(int routeWeight) {
		this.routeWeight = routeWeight;
	}

	public int getRouteWeight() {
		return routeWeight;
	}
	
	@Override
	public String toString() {
		return "Car [curPos=" + curPos + ", curPosIndex=" + curPosIndex
				+ ", destPos=" + destPos + ", destPosIndex=" + destPosIndex
				+ ", targetStreet=" + targetStreet + ", routeWeight="
				+ routeWeight + ", skin=" + skin + ", lifeTime=" + waitingTime
				+ ", moved=" + moved + ", stepStill=" + idleTime + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((curPos == null) ? 0 : curPos.hashCode());
		result = prime * result + curPosIndex;
		result = prime * result + ((destPos == null) ? 0 : destPos.hashCode());
		result = prime * result + destPosIndex;
		result = prime * result + waitingTime;
		result = prime * result + (moved ? 1231 : 1237);
		result = prime * result + routeWeight;
		result = prime * result + skin;
		result = prime * result + idleTime;
		result = prime * result
				+ ((targetStreet == null) ? 0 : targetStreet.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Car other = (Car) obj;
		if (curPos == null) {
			if (other.curPos != null)
				return false;
		} else if (!curPos.equals(other.curPos))
			return false;
		if (curPosIndex != other.curPosIndex)
			return false;
		if (destPos == null) {
			if (other.destPos != null)
				return false;
		} else if (!destPos.equals(other.destPos))
			return false;
		if (destPosIndex != other.destPosIndex)
			return false;
		if (waitingTime != other.waitingTime)
			return false;
		if (moved != other.moved)
			return false;
		if (routeWeight != other.routeWeight)
			return false;
		if (skin != other.skin)
			return false;
		if (idleTime != other.idleTime)
			return false;
		if (targetStreet == null) {
			if (other.targetStreet != null)
				return false;
		} else if (!targetStreet.equals(other.targetStreet))
			return false;
		return true;
	}

	public void crossStreet() {
		this.setCurPosIndex(0);
		this.setCurPos(this.getTargetStreet());
		this.setMoved(true);
	}
	
	
}
